package com.css.zfzx.sjcj.modules.hddcB7_VolcanicSampleResultTable.controller;

import com.alibaba.fastjson.JSONObject;
import com.css.bpm.platform.base.response.RestResponse;
import com.css.zfzx.sjcj.modules.hddcB7_VolcanicSampleResultTable.repository.entity.HddcB7VolcanicsampleresulttableEntity;
import com.css.zfzx.sjcj.modules.hddcB7_VolcanicSampleResultTable.service.HddcB7VolcanicsampleresulttableService;
import com.css.zfzx.sjcj.modules.hddcB7_VolcanicSampleResultTable.viewobjects.HddcB7VolcanicsampleresulttableQueryParams;
import com.css.bpm.platform.utils.PlatformPageUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Arrays;
import java.util.List;

/**
 * @author zhangcong
 * @date 2020-11-26
 */
@Slf4j
@RestController
@RequestMapping("/hddc/hddcB7Volcanicsampleresulttables")
public class HddcB7VolcanicsampleresulttableController {
    @Autowired
    private HddcB7VolcanicsampleresulttableService hddcB7VolcanicsampleresulttableService;

    @GetMapping("/queryHddcB7Volcanicsampleresulttables")
    public RestResponse queryHddcB7Volcanicsampleresulttables(HttpServletRequest request, HddcB7VolcanicsampleresulttableQueryParams queryParams) {
        RestResponse response = null;
        try{
            int curPage = Integer.valueOf(request.getParameter(PlatformPageUtils.CUR_PAGE));
            int pageSize = Integer.valueOf(request.getParameter(PlatformPageUtils.PAGE_SIZE));
            String sort= request.getParameter("sort");
            String order= request.getParameter("order");
            JSONObject jsonObject = hddcB7VolcanicsampleresulttableService.queryHddcB7Volcanicsampleresulttables(queryParams,curPage,pageSize,sort,order);
            response = RestResponse.succeed(jsonObject);
        }catch (Exception e){
            String errorMessage = "查询失败!";
            log.error(errorMessage,e);
            response = RestResponse.fail(errorMessage);
        }
        return response;
    }

    @GetMapping("{id}")
    public RestResponse getHddcB7Volcanicsampleresulttable(@PathVariable String id) {
        RestResponse response = null;
        try{
            HddcB7VolcanicsampleresulttableEntity hddcB7Volcanicsampleresulttable = hddcB7VolcanicsampleresulttableService.getHddcB7Volcanicsampleresulttable(id);
            response = RestResponse.succeed(hddcB7Volcanicsampleresulttable);
        }catch (Exception e){
            String errorMessage = "获取失败!";
            log.error(errorMessage,e);
            response = RestResponse.fail(errorMessage);
        }
        return response;
    }
    @PostMapping
    public RestResponse saveHddcB7Volcanicsampleresulttable(@RequestBody HddcB7VolcanicsampleresulttableEntity hddcB7Volcanicsampleresulttable) {
        RestResponse response = null;
        JSONObject json = new JSONObject();
        try{
            hddcB7VolcanicsampleresulttableService.saveHddcB7Volcanicsampleresulttable(hddcB7Volcanicsampleresulttable);
            json.put("message", "新增成功!");
            response = RestResponse.succeed(json);
        }catch (Exception e){
            String errorMessage = "新增失败!";
            log.error(errorMessage,e);
            response = RestResponse.fail(errorMessage);
        }
        return response;

    }
    @PostMapping("/update")
    public RestResponse updateHddcB7Volcanicsampleresulttable(@RequestBody HddcB7VolcanicsampleresulttableEntity hddcB7Volcanicsampleresulttable)  {
        RestResponse response = null;
        JSONObject json = new JSONObject();
        try{
            hddcB7VolcanicsampleresulttableService.updateHddcB7Volcanicsampleresulttable(hddcB7Volcanicsampleresulttable);
            json.put("message", "修改成功!");
            response = RestResponse.succeed(json);
        }catch (Exception e){
            String errorMessage = "修改失败!";
            log.error(errorMessage,e);
            response = RestResponse.fail(errorMessage);
        }
        return response;
    }
    @PostMapping("/delete")
    public RestResponse deleteHddcB7Volcanicsampleresulttables(@RequestBody String ids) {
        RestResponse response = null;
        JSONObject json = new JSONObject();
        try{
            hddcB7VolcanicsampleresulttableService.deleteHddcB7Volcanicsampleresulttables(Arrays.asList(ids.split(",")));
            json.put("message", "删除成功!");
            response = RestResponse.succeed(json);
        }catch (Exception e){
            String errorMessage = "删除失败!";
            log.error(errorMessage,e);
            response = RestResponse.fail(errorMessage);
        }
        return response;
    }

    @GetMapping("/getValidDictItemsByDictCode/{dictCode}")
    public RestResponse getValidDictItemsByDictCode(@PathVariable String dictCode) {
        RestResponse restResponse = null;
        try {
            restResponse = RestResponse.succeed(hddcB7VolcanicsampleresulttableService.getValidDictItemsByDictCode(dictCode));
        } catch (Exception e) {
            String errorMsg = "字典项获取失败!";
            log.error(errorMsg, e);
            restResponse = RestResponse.fail(errorMsg);
        }
        return restResponse;
    }
    /***
     * 导出
     * @param response
     * @return
     */
    @GetMapping("/exportFile")
    public RestResponse exportFileYhDisasters(HttpServletResponse response,
                                              @RequestParam("projectName")String projectName,
                                              @RequestParam("province") String province,@RequestParam("city")String city,@RequestParam("area")String area,@RequestParam("qualityinspectionStatus")String qualityinspectionStatus) {
        RestResponse responseRest = null;
        JSONObject jsonObject = new JSONObject();
        try{
            HddcB7VolcanicsampleresulttableQueryParams queryParams=new HddcB7VolcanicsampleresulttableQueryParams();
            queryParams.setArea(area);
            queryParams.setCity(city);
            queryParams.setProvince(province);
            queryParams.setProjectName(projectName);
            queryParams.setQualityinspectionStatus(qualityinspectionStatus);
            hddcB7VolcanicsampleresulttableService.exportFile(queryParams,response);
            jsonObject.put("message", "导出成功");
            responseRest = RestResponse.succeed(jsonObject);
        }catch (Exception e){
            String errorMessage = "查询失败!";
            log.error(errorMessage,e);
            responseRest = RestResponse.fail(errorMessage);
        }
        return responseRest;
    }

    /**
     * 导入
     *
     * @param file
     * @param response
     * @return
     */
    @PostMapping("/importDisaster")
    public RestResponse export(@RequestParam("file") MultipartFile file, HttpServletResponse response) {
        RestResponse restResponse = null;
        try {
            String s = hddcB7VolcanicsampleresulttableService.exportExcel( file, response);
            restResponse = RestResponse.succeed(s);
        } catch (Exception e) {
            String errormessage = "导入失败";
            log.error(errormessage, e);
            restResponse = RestResponse.fail(errormessage);
        }
        return restResponse;
    }
}